<?php

if(isset($_GET['do'])){
	include("inc/class.otr.php");
	$otr = new OTR();
	if($_GET['do']=='insert'){
		$task_id = $_GET['taskid'];
		$task = $_GET['task'];
		$prio = $_GET['prio'];
		$owner = $_GET['owner'];
		$sql = "Insert into `tasks` (`related_id`, `related_table`, `task`, `owner_id`, `prio`, `ordinal`, `bit_completed`, `completed_on`)
				Values (".$task_id.", 'tasks', '".$task."', ".$owner.", ".$prio.", 0, 0, '0000-00-00 00:00:00')";
		mysql_query($sql) or die("SQL Error: ".mysql_error()."<br/>".$sql);
		//
		echo("<script type='text/javascript'>ajaxGet('subtasks.php', 'load_from_ajax_id=".$task_id."', $('div_subtask_".$task_id."'), null);</script>");
		exit();
	}else{
		if($_GET['what']=='bit_completed'){
			$sql = "Update `tasks` Set `bit_completed`=1, `completed_on`=NOW() Where `id`=".$_GET['id'];
		} else {
			$sql = "Update `tasks` Set `".$_GET['what']."`='".mysql_escape_string($_GET['data'])."' Where `id`=".$_GET['id'];
		}
		echo($sql."<br/>");
		//$sql = mysql_escape_string($sql);
		if(mysql_query($sql) or die("SQL Error: ".mysql_error())){
			echo "Task updated!";
			exit();
		} else {
			echo "Failed updating task!";
			exit();
		}
	}
}

if(isset($_REQUEST['load_from_ajax_id'])){
	include("inc/class.otr.php");
	$otr = new OTR();
	$task_id = $_REQUEST['load_from_ajax_id'];
	$users = $otr->getUsers();
}else{
	$task_id = $o['id'];
}
//get subtasks
$sub = $otr->getSubTasks($task_id);

?>
<table cellspacing="0" cellpadding="1px" style="color: black;">
	<tbody>
<?php
foreach($sub as $s){
?>
	<tr style="border-bottom: black 1px dotted; padding: 2px;">
		<td>
			<img src="imgs/dots.gif" />
			<input type="hidden" id="txt_id" value="<?php echo($s['id']);?>"/>
			<input type="hidden" id="ordinal" value="<?php echo($s['ordinal']);?>" style="width: 20px;"/>
		</td>
		<td><input readonly="readonly" onblur="exitEdit(<?php echo($s['id']);?>, this);" type="text" id="txt_task" value='<?php echo($s["task"]);?>' ondblclick="goEdit(this);" style="width: 300px;"/></td>
		<td><select id="ddl_owner" style="width: 60px; border: 1px solid gray;" onchange="updateTask(<?php echo($s['id']);?>, 'owner_id', this);">
<?php
	foreach($users as $u){
		$selected = '';
		if($u['id']==$s['owner_id']) $selected = 'selected="selected"';
?>
		<option <?php echo($selected);?> value="<?php echo($u['id']);?>"><?php echo($u['initials']);?></option>
<?php
	}
?>
		</select></td>
		<td><select id="ddl_prio" style="width: 40px; border: 1px solid gray;" onchange="updateTask(<?php echo($s['id']);?>, 'prio', this);" class="<?php echo($otr->getPrioCSS($s['prio'], $s['bit_completed']));?>">
			<option class="prio_low" value="1" <?php if($s['prio']==1) echo('selected="selected"');?>>&nbsp;</option>
			<option class="prio_medium" value="2" <?php if($s['prio']==2) echo('selected="selected"');?>>&nbsp;</option>
			<option class="prio_high" value="3" <?php if($s['prio']==3) echo('selected="selected"');?>>&nbsp;</option>
			<option class="prio_mad" value="4" <?php if($s['prio']==4) echo('selected="selected"');?>>&nbsp;</option>
		</select></td>
		<!--<td style="text-align: right;"><?php //echo($otr->getTaskTime($s['id']));?></td>-->
		<td style="text-align: right;"><?php echo($s['time']);?></td>
<?php
	if($s['bit_completed']==1){
		//task completed
?>
		<td style="text-align: right;"><input style="border: none;" readonly="readonly" id="txt_completed" value="<?php echo($s['completed_on']);?>" /></td>
<?php
	}else{
		//task NOT completed
?>
		<td>
			<input type="button" style="background-image: url(imgs/btnCompleted.gif); background-repeat: no-repeat;" onclick="updateTask(<?php echo($s['id']);?>, 'bit_completed', 1);" />
			<input type="button" style="background-image: url(imgs/punch_v1.gif); background-repeat: no-repeat;" onclick="punch(<?php echo($s['id']);?>);" />
		</td>
		<td><img src="imgs/clockv2.gif" id="img_punch_<?php echo($s['id']);?>" style="display: none; border: none;" /></td>
		<td><div id="txt_clock_<?php echo($s['id']);?>" /></td>
		<td><input style="display: none; background-color: #fff; border: 1px solid #000;" type="text" id="txt_punchnote_<?php echo($s['id']);?>" /></td>
		<td><input type="button" style="display: none;" value="!" id="btn_punchnote_<?php echo($s['id']);?>" onclick="updatePunch(this);"/></td>
<?php		
	}
?>
		</tr>
<?php
}
?>
		<tr>
			<td colspan="6" style="text-align: right;">
				<input type="button" value="add subtask" onclick="showAddRow('addSubRow_<?php echo($task_id);?>');"/>
			</td>
		</tr>
	<tr class="" id="addSubRow_<?php echo($task_id);?>" style="display: none; background-color: silver;">
		<td>
			<input type="hidden" id="txt_id" value="0"/>
			<input type="hidden" id="ordinal" value="" style="width: 20px;"/>
		</td>
		<td><input type="text" id="addsub_txt_task_<?php echo($task_id);?>" value="" style="width: 300px; border-bottom: 1px solid gray; background-color: white;"/></td>
		<td><select id="addsub_ddl_owner_<?php echo($task_id);?>" style="width: 60px; border: 1px solid gray;">
<?php
	foreach($users as $u){
?>
			<option value="<?php echo($u['id']);?>"><?php echo($u['initials']);?></option>
<?php
	}
?>
		</select></td>
		<td colspan="2"><select id="addsub_ddl_prio_<?php echo($task_id);?>" style="width: 60px; border: 1px solid gray;">
			<option class="prio_low" value="1">low</option>
			<option class="prio_medium" value="2">medium</option>
			<option class="prio_high" value="3">high</option>
			<option class="prio_mad" value="4">mad</option>
		</select></td>
		<td style="text-align: right;">
			<input type="button" value="save" onclick="insertNewSubTask(<?php echo($task_id);?>);"/>
			<input type="button" value="cancel" onclick="exitAdd('addSubRow');"/>
		</td>
	</tr>
	</table>
</div>
